﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Digitalmes.Model.IoTManagement;
using Digitalmes.Model;

namespace Digitalmes.ViewModel.IoTManagement.MeterDataRecordVMs
{
    public partial class MeterDataRecordListVM : BasePagedListVM<MeterDataRecord_View, MeterDataRecordSearcher>
    {
        
        protected override IEnumerable<IGridColumn<MeterDataRecord_View>> InitGridHeader()
        {
            return new List<GridColumn<MeterDataRecord_View>>{
                
                this.MakeGridHeader(x => x.MeterDataRecord_Device).SetTitle(@Localizer["Page.设备"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_Source).SetTitle(@Localizer["Page.数据来源"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_CollectStatus).SetTitle(@Localizer["Page.采集状态"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_IsAbnormal).SetTitle(@Localizer["Page.异常标记"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_AbnormalReason).SetTitle(@Localizer["Page.异常原因"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_CurrentValue).SetTitle(@Localizer["Page.当前读数"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_Parameter).SetTitle(@Localizer["Page.数据"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_ParameterName).SetTitle(@Localizer["Page.数据名称"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_DataValueType).SetTitle(@Localizer["Page.数据类型"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_Usage).SetTitle(@Localizer["Page.本次用量"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_Unit).SetTitle(@Localizer["Page.单位"].Value),
                this.MakeGridHeader(x => x.MeterDataRecord_ReadingTime).SetTitle(@Localizer["Page.读数时间"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<MeterDataRecord_View> GetSearchQuery()
        {
            var query = DC.Set<MeterDataRecord>()
                
                .CheckEqual(Searcher.DeviceId, x=>x.DeviceId)
                .CheckEqual(Searcher.Source, x=>x.Source)
                .CheckEqual(Searcher.CollectStatus, x=>x.CollectStatus)
                .CheckEqual(Searcher.IsAbnormal, x=>x.IsAbnormal)
                .CheckContain(Searcher.AbnormalReason, x=>x.AbnormalReason)
                .CheckEqual(Searcher.CurrentValue, x=>x.CurrentValue)
                .CheckEqual(Searcher.ParameterId, x=>x.ParameterId)
                .CheckContain(Searcher.ParameterName, x=>x.ParameterName)
                .CheckEqual(Searcher.DataValueType, x=>x.DataValueType)
                .CheckEqual(Searcher.Usage, x=>x.Usage)
                .CheckContain(Searcher.Unit, x=>x.Unit)
                .CheckBetween(Searcher.ReadingTime?.GetStartTime(), Searcher.ReadingTime?.GetEndTime(), x => x.ReadingTime, includeMax: false)
                .Select(x => new MeterDataRecord_View
                {
				    ID = x.ID,
                    
                    MeterDataRecord_Device = x.Device.DeviceCode,
                    MeterDataRecord_Source = x.Source,
                    MeterDataRecord_CollectStatus = x.CollectStatus,
                    MeterDataRecord_IsAbnormal = x.IsAbnormal,
                    MeterDataRecord_AbnormalReason = x.AbnormalReason,
                    MeterDataRecord_CurrentValue = x.CurrentValue,
                    MeterDataRecord_Parameter = x.Parameter.Description,
                    MeterDataRecord_ParameterName = x.ParameterName,
                    MeterDataRecord_DataValueType = x.DataValueType,
                    MeterDataRecord_Usage = x.Usage,
                    MeterDataRecord_Unit = x.Unit,
                    MeterDataRecord_ReadingTime = x.ReadingTime,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class MeterDataRecord_View: MeterDataRecord
    {
        
        public string MeterDataRecord_Device { get; set; }
        public DataSourceEnum? MeterDataRecord_Source { get; set; }
        public CollectStatusEnum? MeterDataRecord_CollectStatus { get; set; }
        public bool? MeterDataRecord_IsAbnormal { get; set; }
        public string MeterDataRecord_AbnormalReason { get; set; }
        public decimal? MeterDataRecord_CurrentValue { get; set; }
        public string MeterDataRecord_Parameter { get; set; }
        public string MeterDataRecord_ParameterName { get; set; }
        public TagDataTypeEnum? MeterDataRecord_DataValueType { get; set; }
        public decimal? MeterDataRecord_Usage { get; set; }
        public string MeterDataRecord_Unit { get; set; }
        public DateTime? MeterDataRecord_ReadingTime { get; set; }

    }

}